<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function res(S) {
      S = S.split('');
      //双指针
      var reg = /[a-zA-Z]/;
      for (var i = 0, j = S.length - 1; i < j;) {
        if (!reg.test(S[i])) i++;
        if (!reg.test(S[j])) j--;
        if (reg.test(S[i]) && reg.test(S[j])) {
          var temp = S[i];
          S[i] = S[j];
          S[j] = temp;
          i++;
          j--;
        }
      }
      return S.join('');
    };
    console.log(res("ab-cd"));
  </script>
</body>

</html>